import { Body, Controller, Get, Post, Res, HttpStatus, UploadedFile, UseInterceptors, HttpException, Query,Request } from '@nestjs/common';
import { FileInterceptor } from '@nestjs/platform-express';
import e, { Response } from 'express';
import { Profit } from './profit.entity';
import { ProfitService } from './profit.service';
import { WxUserService } from '../wxUser/wxUser.service';
import { AnswerService } from '../answer/answer.service';
import {
  profitEnum
} from './profit.enum'
import {
  get,
  post
} from 'request'
import request from 'request'
import {join, resolve} from 'path'
import { createWriteStream } from 'fs';
import { ValidTokenInterceptor } from '../../interceptor/validToken'
import { ValidRSAInterceptor } from '../../interceptor/validRSA'
import utils from '../../common/utils'

import {
  existsSync,
  mkdirSync
} from 'fs'

import {
  apibefore
} from '../../router'
import { MultiImgService } from '../multiImg/multiImg.service';
import { Answer } from '../answer/answer.entity';
import { AnswerInfo } from '../answerInfo/answerInfo.entity';
import { AnswerInfoService } from '../answerInfo/answerInfo.service';
import { UserService } from '../user/user.service';
import { RedisInstance } from 'src/common/redis';
import { WXAPI } from 'src/common/wx_api';
import { ValidUniqueLoginInterceptor } from 'src/interceptor/validUniqueLogin';
@Controller(apibefore('/profit_background'))
export class ProfitControllerBackground {
  constructor(private readonly service: ProfitService,
    private readonly userService: UserService
    ) {}

  
    
  /**
   * 查询视频下所有退款申请
   * @param params 
   * @param req 
   * @returns 
   */
  @Get('refund_list')
  async refundList(@Query() params,@Request() req){
    let findWxUser = await this.userService.findOne(req.headers.token)
    if(!findWxUser){
      return new HttpException('用户不存在',HttpStatus.INTERNAL_SERVER_ERROR)
    }
    let { skip, take } = utils.getLimit(params.pageSize, params.pageIndex)
    let result = await this.service.refundList(skip, take, params.video_id)
    result.data.filter((e:any) => e.video_url || e.video_url_source).map((e:any) => {
      if(e.video_url_source){
        e.video_url = e.video_url_source
      }
      utils.getPlayVideoUrl(e)
    })
    return result
  }


  /**
   * 查询视频下所有退款申请
   * @param params 
   * @param req 
   * @returns 
   */
  @Get('refund_list_by_video_id')
  async refundListByVideoId(@Query() params,@Request() req){
    let findWxUser = await this.userService.findOne(req.headers.token)
    if(!findWxUser){
      return new HttpException('用户不存在',HttpStatus.INTERNAL_SERVER_ERROR)
    }
    let { skip, take } = utils.getLimit(params.pageSize, params.pageIndex)
    return await this.service.refundListByVideoId(skip, take ,params.video_id)
  }



  /**
   * 同意退款
   * @param data 
   * @param req 
   */
  @Post('refund_success')
  @UseInterceptors(ValidUniqueLoginInterceptor)
  async refundSuccess(@Body() data,@Request() req){
    let findWxUser = await this.userService.findOne(req.headers.token)
    if(!findWxUser){
      return new HttpException('用户不存在',HttpStatus.INTERNAL_SERVER_ERROR)
    }
    const answer_id = data.answer_id

    const err = await this.service.refundSuccess(data.id)
    if(err){
      return new HttpException(err, HttpStatus.INTERNAL_SERVER_ERROR)
    }else{
      new WXAPI().clearPayWallet(data.wx_user)
      return true
    }
  }

  /**
   * 拒绝退款
   * @param data 
   * @param req 
   */
  @Post('refund_reject')
  @UseInterceptors(ValidUniqueLoginInterceptor)
  async refundReject(@Body() data,@Request() req){
    let findWxUser = await this.userService.findOne(req.headers.token)
    if(!findWxUser){
      return new HttpException('用户不存在',HttpStatus.INTERNAL_SERVER_ERROR)
    }
    const result = await this.service.refundReject(data.id, data.reject_reason)
    return result
  }
}
